﻿namespace EyeOpen.IO.Test
{
	using System.Collections.Generic;
	using System.IO;
	using System.Linq;

	internal class TestDirectoryInfo 
		: IDirectoryInfo
	{
		private readonly HashSet<TestDirectoryInfo> directoryList =
			new HashSet<TestDirectoryInfo>();

		private readonly HashSet<FileInfo> fileList =
			new HashSet<FileInfo>();

		public TestDirectoryInfo(string fullName)
		{
			FullName = fullName;
		}

		public TestDirectoryInfo(TestDirectoryInfo parent, string name)
		{
			Parent = parent;

			FullName = Path.Combine(parent.FullName, name);
		}

		public TestDirectoryInfo Parent { get; set; }

		public string FullName
		{
			get; private set;
		}

		public IEnumerable<FileInfo> GetFiles(string fileName)
		{
			return fileList.Where(f => f.Name == fileName);
		}

		public IEnumerable<IDirectoryInfo> GetDirectories()
		{
			return directoryList;
		}

		public TestDirectoryInfo AddDirectory(string name)
		{
			var directoryInfo = new TestDirectoryInfo(this, name);

			directoryList.Add(directoryInfo);

			return directoryInfo;
		}

		public TestDirectoryInfo AddFile(string fileName)
		{
			var fileInfo = new FileInfo(Path.Combine(FullName, fileName));

			fileList.Add(fileInfo);

			return this;
		}

		public override string ToString()
		{
			return FullName;
		}
	}
}